package com.lemon.smartsafety.ui.activity.notice;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.entity.event.notice.NoticeRefreshEvent;
import com.lemon.smartsafety.http.UrlInfo;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.ui.web.BaseWebView;
import com.lemon.smartsafety.ui.web.MyWebChromeClient;
import com.lemon.smartsafety.utils.app.SharedPreferencesServer;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.smartsafetynew.entity.notice.NoticeInfoListVo;
import com.lemon.smartsafetynew.ui.adapter.notice.NoticeInfoLIstAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Cooliean on 16/4/8.
 */
public class NoticeDetailActivity extends SwipeBackActivity {

    @Bind(R.id.webLayout)
    FrameLayout mWebLayout;
    //WebView mWebView;

    String sId = "";
    boolean bHasRead = false;
    @Bind(R.id.baseWebView)
    BaseWebView baseWebView;
    @Bind(R.id.listview)
    ListView listView;
    private List<NoticeInfoListVo> datas;
    private NoticeInfoLIstAdapter adapter;
    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();
        if (getIntent().getExtras() != null) {
            sId = getIntent().getStringExtra(BundleKey.NOTICE_ID);
            bHasRead = getIntent().getBooleanExtra(BundleKey.HAS_READ, false);
        }
        sId = StringUtils.nullValue(sId);
        getMyData();
    }

    //获取附件信息
    private void getMyData() {
        datas = new ArrayList<>();
        NoticeInfoListVo listVo1 = new NoticeInfoListVo();
        listVo1.setName("常用化学品");
        listVo1.setUrl("http://www.wxaj.net/files/safe/M.doc");
        NoticeInfoListVo listVo2 = new NoticeInfoListVo();
        listVo2.setName("建筑设计防火");
        listVo2.setUrl("http://www.wxaj.net/files/safe/03.doc");
        NoticeInfoListVo listVo3 = new NoticeInfoListVo();
        listVo3.setName("焊接和切割安全");
        listVo3.setUrl("http://www.wxaj.net/files/safe/4F.doc");
        NoticeInfoListVo listVo4 = new NoticeInfoListVo();
        listVo4.setName("安全标志和使用");
        listVo4.setUrl("http://www.wxaj.net/files/safe/JY.doc");
        NoticeInfoListVo listVo5 = new NoticeInfoListVo();
        listVo5.setName("隐患排查治理标准");
        listVo5.setUrl("http://www.wxaj.net/files/safe/N.doc");
        datas.add(listVo1);
        datas.add(listVo2);
        datas.add(listVo3);
        datas.add(listVo4);
        datas.add(listVo5);
        adapter = new NoticeInfoLIstAdapter(datas,this);
        listView.setAdapter(adapter);
    }

    @Override
    protected void initListener() {
        super.initListener();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Log.i("lala",datas.get(i).getName());
            }
        });
    }

    @Override
    protected void initViews() {
        super.initViews();

        WebSettings ws = baseWebView.getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setDomStorageEnabled(true);

        String appCachePath = this.getCacheDir().getAbsolutePath();
        ws.setAppCachePath(appCachePath);
        ws.setDatabaseEnabled(true);
        String dbPath = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
        ws.setDatabasePath(dbPath);
        ws.setDomStorageEnabled(true);

        ws.setCacheMode(WebSettings.LOAD_NO_CACHE);
        ws.setAllowFileAccess(true);
        ws.setAppCacheEnabled(false);


        baseWebView.setWebChromeClient(new MyWebChromeClient());
        baseWebView.setWebViewClient(new WebViewClient());
        baseWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

        baseWebView.setBackgroundColor(0);
        //mWebLayout.addView(mWebView);
        String url = new SharedPreferencesServer(getApplicationContext()).getRequestUrl(UrlInfo.VIEW_NOTICE);
        url += "?rp=" + Constants.RP_PASSWORD + "&notice_id=" + sId + "&user_id=" + mSharedPreferencesHelper.getUserId();
        baseWebView.loadUrl(url);
    }

    @Override
    protected void initTitle() {
        setBarTitle("通知公告");
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(NoticeDetailActivity.this);
        if (!bHasRead) {
            sendRefreshEvent();
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            callFinish();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_notice_detail;
    }

    private void sendRefreshEvent() {
        NoticeRefreshEvent event = new NoticeRefreshEvent();
        AppService.getBus().post(event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWebLayout != null) {
            mWebLayout.removeAllViews();
            baseWebView.destroy();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
